# -*- coding: utf-8 -*-
# @Time    : 2019/3/12 7:36
# @Author  : Roger Lee
# @Email   : 704482843@qq.com
# @File    : utils.py
# @Software: PyCharm
from urllib import parse
import hmac

import hashlib
import binascii


def getpackStr():
    data_list = [{"key": "appCodeName", "value": "Mozilla"}, {"key": "appMinorVersion", "value": ""},
                 {"key": "appName", "value": "Netscape"}, {"key": "appVersion",
                                                           "value": "5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) QtWebEngine/5.6.3 Chrome/49.0.2623.111 Safari/537.36"},
                 {"key": "browserLanguage", "value": "zh"}, {"key": "cookieCode"},
                 {"key": "cookieEnabled", "value": "true"}, {"key": "cpuClass", "value": ""},
                 {"key": "flashVersion", "value": 0}, {"key": "historyList", "value": 1},
                 {"key": "javaEnabled", "value": "false"}, {"key": "mimeTypes", "value": ""},
                 {"key": "onLine", "value": "true"}, {"key": "partnerCode", "value": "123"},
                 {"key": "platform", "value": "5"}, {"key": "scrAvailHeight", "value": "1050"},
                 {"key": "scrAvailWidth", "value": "1920"}, {"key": "scrColorDepth", "value": "32"},
                 {"key": "scrDeviceXDPI", "value": ""}, {"key": "scrHeight", "value": "1080"},
                 {"key": "scrWidth", "value": "1920"}, {"key": "sdkVersion", "value": "3.1.0"},
                 {"key": "systemLanguage", "value": ""}, {"key": "timeZone", "value": -8}, {"key": "userAgent",
                                                                                            "value": "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) QtWebEngine/5.6.3 Chrome/49.0.2623.111 Safari/537.36"},
                 {"key": "userLanguage", "value": ""}]
    query_string = ''
    for data in data_list:
        key = str(data.get("key", "")).encode("utf-8")
        value = str(data.get("value", "")).upper().encode("utf-8")

        if str(data.get("key", "")) == "cookieCode":
            value = "UNDEFINED".upper().encode("utf-8")
        query_string += parse.quote(key) + "=" + parse.quote(value) + "&"
    query_string = query_string.replace("%28", "(").replace("%29", ")").replace("/", "%2F")
    sha256 = hashlib.sha256()

    sha256.update(query_string[:-1].encode('utf-8'))
    res = sha256.hexdigest()

    query_string += "sign=" + res

    return query_string


def crc32(v):
    """
    Generates the crc32 hash of the v.
    @return: str, the str value for the crc32 of the v
    """
    return '%x' % (binascii.crc32(v.encode("utf-8")) & 0xffffffff)  # 取crc32的八位数据 %x返回16进制


def getSignature(partnerCode, platform):
    secretKey = "9cWbNgUqiL91raHPVmrP"  # _0xa20c('0x895', 'M(d%');
    packStr = getpackStr()

    data = 'packageStr=' + parse.quote(packStr.encode("utf-8")).replace("%28", "(").replace("%29",
                                                                                            ")") + '&partnerCode=' + parse.quote(
        partnerCode.encode("utf-8")) + "&platform=" + parse.quote(platform.encode("utf-8"))

    s = get_hmac(secretKey.encode("utf-8"), data.encode("utf-8"))
    scrc = crc32(s)

    res = s[0:10] + scrc[:2] + s[10:20] + scrc[2:4] + s[14:24] + scrc[4:6] + s[30:40] + scrc[6:8]

    return res


def get_hmac(secret, data):
    h = hmac.new(secret, data, hashlib.sha1)
    return h.hexdigest()


def get_g_sign(signature):
    m = hashlib.md5()
    m.update(signature.encode('utf-8'))

    return m.hexdigest()
